/**
*   $("#startone").scoreFun();
    $("#starttwo").scoreFun({
       fen_d:22,//每一个a的宽度
       ScoreGrade:5//a的个数 10或者
    });
*
*/

(function(factory){
    if(typeof define === 'function' && define.amd){
        define(['jquery'],factory);
    }else{
        factory(window.jQuery);
    }
}(function($){
    
    $.fn.scoreFun = function (opts) {
        var defaults = {
            fen_d: 70,
            ScoreGrade: 10,
            types: [],
            nameScore: "fenshu",
            parent: "star_score",
            attitude: "attitude"
        };
        var options = $.extend({},defaults,opts);
        return this.each(function() {
            var t = $(this);
            var countScore = t.find("." + options.nameScore);
            var startParent = t.find("." + options.parent);
            var atti = t.find("." + options.attitude);
            var now_cli;
            var fen_cli;
            var atu;
            var fen_d = options.fen_d;
            var len = options.ScoreGrade;
            startParent.css({width:(fen_d * len)/64+'rem'});
            var preA = (5 / len);
            for (var i = 0; i < len; i++) {
                var newSpan = $("<a href='javascript:void(0)'></a>");
                newSpan.css({
                    "left": 0,
                    "width": (fen_d * (i + 1))/64+'rem',
                    "z-index": len - i
                });
                newSpan.appendTo(startParent);
            }
            startParent.find("a").each(function(index, element) {
                $(this).click(function() {
                    now_cli = index;
                    show(index, $(this));
                });
                $(this).mouseenter(function() {
                    show(index, $(this));
                });
                $(this).mouseleave(function() {
                    if (now_cli >= 0) {
                        var scor = preA * (parseInt(now_cli) + 1);
                        startParent.find("a").removeClass("clibg");
                        startParent.find("a").eq(now_cli).addClass("clibg");
                        var ww = fen_d * (parseInt(now_cli) + 1);
                        startParent.find("a").eq(now_cli).css({
                            "width": ww/64+'rem',
                            "left": "0"
                        });
                        if (countScore) {
                            countScore.text(scor);
                        }
                    } else {
                        startParent.find("a").removeClass("clibg");
                        if (countScore) {
                            countScore.text("");
                        }
                    }
                });
            });
            function show(num, obj) {
                var n = parseInt(num) + 1;
                var lefta = num * fen_d;
                var ww = fen_d * n;
                var scor = preA * n;
                atu = options.types[parseInt(num)];
                t.find("a").removeClass("clibg");
                obj.addClass("clibg");
                obj.css({
                    "width": ww/64+'rem',
                    "left": "0"
                });
                countScore.text(scor);
                atti.text(atu);
            }
        });
    }
}));